#include "item.h"
#include "frameFactory.h"

Item::Item(const Vector2f& pos, const Vector2f& vel,const Frame* f):
Sprite(pos, vel, f),type(SPEEDU){}

Item::Item(const Sprite& s,const int t):
Sprite(s.getPosition(),s.getVelocity(),bindFrame(t)),type(t){
	setVelocity(Vector2f(0,100));
}

Frame* Item::bindFrame(int t){
	Frame* f;

	switch(t){
		case SPEEDU:
			f = FrameFactory::getInstance()->getFrame("itemSpeedU","item");
			break;
		case SPEEDD:
			f = FrameFactory::getInstance()->getFrame("itemSpeedD","item");
			break;
		case LENGTHP:
			f = FrameFactory::getInstance()->getFrame("itemLengthP","item");
			break;
		case LENGTHM:
			f = FrameFactory::getInstance()->getFrame("itemLengthM","item");
			break;
		case BALL:
			f = FrameFactory::getInstance()->getFrame("itemBall","item");
			break;
		case LIFE:
			f = FrameFactory::getInstance()->getFrame("itemLife","item");
			break;
		case EXPLOSION:
			f = FrameFactory::getInstance()->getFrame("itemExplosion","item");
			break;
		case NEXT:
			f = FrameFactory::getInstance()->getFrame("itemNext","item");
			break;
	}
	return f;

}


void Item::update(unsigned ticks) { 
  float incr = velocityY() * static_cast<float>(ticks) * 0.001;
  Y( Y()+incr );
  
  if ( Y() < 0) {
    velocityY( abs( velocityY() ) );

  }	
  //float height = static_cast<float>(frame->getHeight());
    //if ( Y() > gameData->getXmlInt("worldHeight")-height) {
    //velocityY( -abs( velocityY() ) );
  //}

  incr = velocityX() * static_cast<float>(ticks) * 0.001;
  X( X()+incr );
  float width = static_cast<float>(frame->getWidth());
  if ( X() < 0) {
    velocityX( abs( velocityX() ) );

  }
  if ( X() >  gameData->getXmlInt("worldWidth")-width) {
    velocityX( -abs( velocityX() ) );

  }
}

void Item::draw() const {
	Sprite::draw();
}
